package hotel.meituan.oversea.biz.task;

import hotel.base.oversea.constant.CommonConstants;
import hotel.base.oversea.constant.enums.MessageEnums;
import hotel.base.oversea.utils.DateUtil;
import hotel.base.oversea.utils.GsonUtils;
import hotel.base.oversea.utils.Result;
import hotel.base.oversea.utils.SendUtils;
import hotel.base.oversea.vo.meituan.MtHotelOrderJobDealVo;
import hotel.base.oversea.vo.meituan.request.goods.MtHotelGoodsBaseInfoVo;
import hotel.base.oversea.vo.meituan.request.goods.MtHotelGoodsChildInfoVo;
import hotel.base.oversea.vo.meituan.request.goods.MtHotelGoodsVo;
import hotel.base.oversea.vo.meituan.request.room.MtHotelRealRoomVo;
import hotel.meituan.oversea.biz.config.service.RedisService;
import hotel.meituan.oversea.biz.service.MtHotelGoodsService;
import hotel.meituan.oversea.biz.service.MtHotelRoomNameService;
import hotel.meituan.oversea.biz.utils.ChineseToSpell;
import hotel.meituan.oversea.biz.utils.SimilarityUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
import org.springframework.util.ObjectUtils;
import org.springframework.web.client.RestTemplate;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;


@Slf4j
@Component
public class MeituanNewOrderCheckSimiTask {
    private String timestamp = "Msalt"+String.valueOf(System.currentTimeMillis());

    @Autowired
    private RestTemplate restTemplate;
    @Autowired
    private MtHotelGoodsService mtHotelGoodsService;
    @Autowired
    private MtHotelRoomNameService mtHotelRoomNameService;
    @Autowired
    private RedisService redisService;

    public static void main(String[] args) {
        String r = "123";
        System.out.printf(""+r.substring(0,r.length()-1));
    }

    //@Async 标识这个方法异步执行，可返回void或Future
    @Async
    public void executeCheckSimi(MtHotelOrderJobDealVo mtHotelOrderJobDealVo){
        //[BOOKING-SIMILARITY] 预下单成功但相似度校验较低 :2021-01-05 19:16:09 【房型名称=大床房】与【产品名称=开 业特惠大床房】，相似度=70%，低于77%请人工关注是否产品与房型信息差异大。订单号=14251973271


        //log.info("MeituanSaveApiLogTask {} >>>>>>>>> 异步线程 保存系统日志  开始执行时间：{}",timestamp,DateUtil.getNowTime());
        String roomsJson = (String)redisService.get("sw_mt_hotel_rooms_data_job_push"+mtHotelOrderJobDealVo.getMtHotelId());
        if(null != roomsJson){
            List<MtHotelRealRoomVo> list = GsonUtils.jsonToList(roomsJson,MtHotelRealRoomVo.class);
            if(null != list && list.size()>0){
                //预下单成功，比较购买产品名称和房型名称的相似度
                Long mtHotelId = Long.valueOf(mtHotelOrderJobDealVo.getMtHotelId());
                List<Long> hotelIds = new ArrayList<>();
                hotelIds.add(mtHotelId);
                Result goodsRs = mtHotelGoodsService.getMtHotelGoods(null,hotelIds,mtHotelOrderJobDealVo.getCheckinDate(),mtHotelOrderJobDealVo.getCheckoutDate(),1);
                if(goodsRs.getCode()==CommonConstants.SUCCESS){
                    MtHotelGoodsVo mtHotelGoodsVo = (MtHotelGoodsVo)goodsRs.getData();
                    if(null != mtHotelGoodsVo){
                        MtHotelGoodsChildInfoVo thisHotelGoodsData = mtHotelGoodsVo.getHotelGoods().get(0);
                        if(null != thisHotelGoodsData && null != thisHotelGoodsData.getGoods() && thisHotelGoodsData.getGoods().size()>0){
                            for (MtHotelGoodsBaseInfoVo c:thisHotelGoodsData.getGoods()){
                                if(new BigDecimal(c.getGoodsId()).compareTo(new BigDecimal(mtHotelOrderJobDealVo.getGoodsId()))==0){
                                    String goodsName = c.getGoodsName();
                                    String goodsid = c.getGoodsId();
                                    String roomName = mtHotelRoomNameService.getRoomNameByGoodsIdHotelId(list,mtHotelOrderJobDealVo.getMtHotelId(),c.getRoomInfoList(),goodsid);
                                    String dingRoomName = roomName;
                                    String dingGoodsName = c.getGoodsName();
                                    String yp1= ChineseToSpell.getPingYin(roomName);
                                    String yp2= ChineseToSpell.getPingYin(goodsName);
                                    Double simiPinyin = SimilarityUtil.getSimilarity(yp1, yp2);
                                    Integer simiIntPinyin = new BigDecimal(simiPinyin).multiply(new BigDecimal(100)).intValue();
                                    if(new BigDecimal(simiIntPinyin).compareTo(new BigDecimal(90))<0){
                                        if(!ObjectUtils.isEmpty(roomName) && !ObjectUtils.isEmpty(goodsName)){
                                            roomName = roomName.replace(" ","");
                                            goodsName = goodsName.replace(" ","");
                                            roomName = roomName.replace("双人","双床");
                                            goodsName = goodsName.replace("双人","双床");
                                            roomName = roomName.replace("单人","单床");
                                            goodsName = goodsName.replace("单人","单床");
                                            if(!roomName.contains("特价") && !roomName.contains("秒杀") && !roomName.contains("特惠") && !roomName.contains("标准") && !roomName.contains("经济") && !roomName.contains("普通") && !roomName.contains("简约")){

                                                if(goodsName.contains(roomName)){//【房型名称=大床房】与【产品名称=开 业特惠大床房】
                                                    break;
                                                }

                                                if(goodsName.contains(roomName.substring(0,roomName.length()-1))){//OOKING-SIMILARITY] 预下单成功但相似度校验较低 :2021-01-05 15:25:33 【房型名称=情侣观景房】与【产品名称=情侣观景大床房（西南医院门诊对面）】，相似度=61%，低于77%请人工关注是否产品与房型信息差异大。订单号=14249912505
                                                    break;
                                                }

                                                String empRoomName = roomName.substring(goodsName.length()-1);
                                                if(!ObjectUtils.isEmpty(empRoomName) && empRoomName.length()==1){//room=标准房，goods=标准间
                                                    break;
                                                }

                                                if(roomName.contains("单人") || roomName.contains("单间") || roomName.contains("单房") || roomName.contains("单床") || roomName.contains("大床")){
                                                    if(goodsName.contains("单人") || goodsName.contains("单间") || goodsName.contains("单房") || goodsName.contains("单床") || roomName.contains("大床")){
                                                        try {
                                                            if(roomName.substring(0,1).equals(goodsName.substring(0,1))){
                                                                break;
                                                            }
                                                        }catch (Exception e){
                                                            e.printStackTrace();
                                                        }
                                                    }
                                                }

                                                if(roomName.contains("双人") || roomName.contains("双间") || roomName.contains("双房") || goodsName.contains("双床")){
                                                    if(goodsName.contains("双人") || goodsName.contains("双间") || goodsName.contains("双房") || goodsName.contains("双床")){
                                                        try {
                                                            if(roomName.substring(0,1).equals(goodsName.substring(0,1))){
                                                                break;
                                                            }
                                                        }catch (Exception e){
                                                            e.printStackTrace();
                                                        }

                                                    }
                                                }

                                                roomName = roomName.toLowerCase();//该房型名称为物理房型名称（物理房型名称是 第一级房型名称，，逻辑房型名称是第二级房型名称，，一对多关系（物理对应逻辑））
                                                goodsName = goodsName.toLowerCase();
                                                try {

                                                    Double simi = SimilarityUtil.getSimilarity(roomName, goodsName);
                                                    Integer simiInt = new BigDecimal(simi).multiply(new BigDecimal(100)).intValue();
                                                    if(null != simiInt){
                                                        log.info("产品房型相似度校验 订单号={}，【roomName="+roomName+"】与【goodsName="+goodsName+"】，相似度={}",mtHotelOrderJobDealVo.getSwOrderId(),simiInt);
                                                        Integer simi_min = 77;//{"key1":"商务大床房","key2":"限量抢商务大床房"} ==  77     {"key1":"豪华商务大床房","key2":"特价商务大床房"} == 75
                                                        if(null != redisService.get("sw_simi_min")){
                                                            simi_min = (Integer)redisService.get("sw_simi_min");
                                                        }
                                                        if(new BigDecimal(simiInt).compareTo(new BigDecimal(simi_min))<0){
                                                            if(!roomName.contains("延迟退房")){
                                                                goodsName = goodsName.replace("延迟退房","");//延迟退房 免停车费  免车位费  自助洗衣   ( )   +   （ ）   每日消毒   延迟退房   /   停车不收费 中央空调 可开普票
                                                            }
                                                            if(!roomName.contains("免停车费")){
                                                                goodsName = goodsName.replace("免停车费","");
                                                            }
                                                            if(!roomName.contains("自助洗衣")){
                                                                goodsName = goodsName.replace("自助洗衣","");
                                                            }
                                                            if(!roomName.contains("每日消毒")){
                                                                goodsName = goodsName.replace("每日消毒","");
                                                            }
                                                            if(!roomName.contains("延迟退房")){
                                                                goodsName = goodsName.replace("延迟退房","");
                                                            }
                                                            if(!roomName.contains("不含空调费")){
                                                                goodsName = goodsName.replace("不含空调费","");
                                                            }
                                                            if(!roomName.contains("不包含空调费")){
                                                                goodsName = goodsName.replace("不包含空调费","");
                                                            }
                                                            if(!roomName.contains("停车不收费")){
                                                                goodsName = goodsName.replace("停车不收费","");
                                                            }
                                                            if(!roomName.contains("中央空调")){
                                                                goodsName = goodsName.replace("中央空调","");
                                                            }
                                                            if(!roomName.contains("可开普票")){
                                                                goodsName = goodsName.replace("可开普票","");
                                                            }
                                                            if(!roomName.contains("部分有窗")){
                                                                goodsName = goodsName.replace("部分有窗","");
                                                            }
                                                            if(!roomName.contains("开业")){
                                                                goodsName = goodsName.replace("开业","");
                                                            }
                                                            if(!roomName.contains("/")){
                                                                goodsName = goodsName.replace("/","");
                                                            }
                                                            if(!roomName.contains("，")){
                                                                goodsName = goodsName.replace("，","");
                                                            }
                                                            if(!roomName.contains("(")){
                                                                goodsName = goodsName.replace("(","");
                                                            }
                                                            if(!roomName.contains(")")){
                                                                goodsName = goodsName.replace(")","");
                                                            }
                                                            if(!roomName.contains("+")){
                                                                goodsName = goodsName.replace("+","");
                                                            }
                                                            if(!roomName.contains("（")){
                                                                goodsName = goodsName.replace("（","");
                                                            }
                                                            if(!roomName.contains("）")){
                                                                goodsName = goodsName.replace("）","");
                                                            }
                                                            try {
                                                                if(new BigDecimal(goodsName.length()).compareTo(new BigDecimal(roomName.length()))>0){
                                                                    //{"key1":"大床电竞房","key2":"大床电竞房(一台电脑❤浴缸❤俛费茶叶、咖啡、矿泉水)"}
                                                                    String shortGN = goodsName.substring(0,roomName.length());
                                                                    if(!shortGN.equals(roomName)){
                                                                        //roomName 大床房(无窗)  goodsName 大床房特惠  相似度=57
                                                                        if(!goodsName.contains("无窗")){
                                                                            roomName = roomName.replace("无窗","");
                                                                        }
                                                                        if(!goodsName.contains("/")){
                                                                            roomName = roomName.replace("/","");
                                                                        }
                                                                        if(!goodsName.contains("，")){
                                                                            roomName = roomName.replace("，","");
                                                                        }
                                                                        if(!goodsName.contains("(")){
                                                                            roomName = roomName.replace("(","");
                                                                        }
                                                                        if(!goodsName.contains(")")){
                                                                            roomName = roomName.replace(")","");
                                                                        }
                                                                        if(!goodsName.contains("+")){
                                                                            roomName = roomName.replace("+","");
                                                                        }
                                                                        if(!goodsName.contains("（")){
                                                                            roomName = roomName.replace("（","");
                                                                        }
                                                                        if(!goodsName.contains("）")){
                                                                            roomName = roomName.replace("）","");
                                                                        }

                                                                        simi = SimilarityUtil.getSimilarity(roomName, goodsName);
                                                                        simiInt = new BigDecimal(simi).multiply(new BigDecimal(100)).intValue();
                                                                        if(new BigDecimal(simiInt).compareTo(new BigDecimal(simi_min))<0){
                                                                            if(new BigDecimal(simiInt).compareTo(new BigDecimal(1))<0){
                                                                                try {
                                                                                    float simiInt2 = SimilarityUtil.getSimilarityRatio(roomName, goodsName);//返回的时乘100后的值
                                                                                    log.info("低于1 getSimilarityRatio再次校验 simiInt2={}，订单号={}",simiInt2,mtHotelOrderJobDealVo.getSwOrderId());
                                                                                    simiInt = new BigDecimal(simiInt2).divide(new BigDecimal(2)).intValue();
                                                                                }catch (Exception e){
                                                                                    e.printStackTrace();
                                                                                }
                                                                            }
                                                                            SendUtils.dingMsgNotLog("[BOOKING-SIMILARITY] 预下单成功但相似度校验较低 ",DateUtil.getCurrTime()+" 【房型名称="+dingRoomName+"】与【产品名称="+dingGoodsName+"】，相似度="+simiInt+"%，低于"+simi_min+"%请人工关注是否产品与房型信息差异大。订单号="+mtHotelOrderJobDealVo.getSwOrderId(), MessageEnums.genre.MT_ORDER_OPERA.getCode(),restTemplate);
                                                                        }
                                                                    }

                                                                }else{
                                                                    //roomName 大床房(无窗)  goodsName 大床房特惠  相似度=57
                                                                    if(!goodsName.contains("无窗")){
                                                                        roomName = roomName.replace("无窗","");
                                                                    }
                                                                    if(!goodsName.contains("/")){
                                                                        roomName = roomName.replace("/","");
                                                                    }
                                                                    if(!goodsName.contains("，")){
                                                                        roomName = roomName.replace("，","");
                                                                    }
                                                                    if(!goodsName.contains("(")){
                                                                        roomName = roomName.replace("(","");
                                                                    }
                                                                    if(!goodsName.contains(")")){
                                                                        roomName = roomName.replace(")","");
                                                                    }
                                                                    if(!goodsName.contains("+")){
                                                                        roomName = roomName.replace("+","");
                                                                    }
                                                                    if(!goodsName.contains("（")){
                                                                        roomName = roomName.replace("（","");
                                                                    }
                                                                    if(!goodsName.contains("）")){
                                                                        roomName = roomName.replace("）","");
                                                                    }

                                                                    simi = SimilarityUtil.getSimilarity(roomName, goodsName);
                                                                    simiInt = new BigDecimal(simi).multiply(new BigDecimal(100)).intValue();
                                                                    if(new BigDecimal(simiInt).compareTo(new BigDecimal(simi_min))<0){
                                                                        if(new BigDecimal(simiInt).compareTo(new BigDecimal(1))<0){
                                                                            try {
                                                                                float simiInt2 = SimilarityUtil.getSimilarityRatio(roomName, goodsName);//返回的时乘100后的值
                                                                                log.info("低于1 getSimilarityRatio再次校验 simiInt2={}，订单号={}",simiInt2,mtHotelOrderJobDealVo.getSwOrderId());
                                                                                simiInt = new BigDecimal(simiInt2).divide(new BigDecimal(2)).intValue();
                                                                            }catch (Exception e){
                                                                                e.printStackTrace();
                                                                            }
                                                                        }
                                                                        SendUtils.dingMsgNotLog("[BOOKING-SIMILARITY] 预下单成功但相似度校验较低 ",DateUtil.getCurrTime()+" 【房型名称="+dingRoomName+"】与【产品名称="+dingGoodsName+"】，相似度="+simiInt+"%，低于"+simi_min+"%请人工关注是否产品与房型信息差异大。订单号="+mtHotelOrderJobDealVo.getSwOrderId(),MessageEnums.genre.SWITCH_ORDER_ROOM_COMPARE_NOTIFY.getCode(),restTemplate);
                                                                    }
                                                                }

                                                            }catch (Exception e){
                                                                e.printStackTrace();
                                                            }

                                                        }
                                                    }
                                                }catch (Exception e){
                                                    log.error("产品房型相似度校验 相似度比较异常："+e.getMessage());
                                                    e.printStackTrace();
                                                }
                                            }

                                        }
                                    }
                                    break;

                                }
                            }
                        }
                    }

                }
            }
        }




    }


}
